#include "stdafx.h"
#include "Graphic.h"
#include"SRC.h"

Graphic::Graphic(CDC * pDC) :pDC(pDC)
{
	pDC->SetBkMode(TRANSPARENT);
	tex_id = -1;
}


Graphic::~Graphic()
{
}

void Graphic::SetViewport(LOGIC_AXIS x, LOGIC_AXIS y)
{
	viewport_x = x;
	viewport_y = y;
}

void Graphic::SetViewportSize(LOGIC_AXIS width, LOGIC_AXIS height)
{
	viewport_width = width;
	viewport_height = height;
}

void Graphic::SetLogicAxis(LOGIC_AXIS x, LOGIC_AXIS y)
{
	pDC->SetViewportOrg(x - viewport_x, y - viewport_y);
}

void Graphic::ResetLogicAxis()
{
	SetLogicAxis(viewport_x, viewport_y);
}

void Graphic::BindTexture(int id)
{
	tex_id = id;
	tex_id = SRC::instance.IsHaveTex(tex_id) ? tex_id : -1;
}

void Graphic::BindTexture(std::wstring path)
{
	tex_id = SRC::instance.GetTexIdWithPath(path);
}

void Graphic::DrawRectangle(LOGIC_AXIS x, LOGIC_AXIS y, LOGIC_AXIS width, LOGIC_AXIS height)
{
	pDC->Rectangle(x, y, x + width, y + height);
}

void Graphic::DrawRectangle(LOGIC_AXIS x, LOGIC_AXIS y, LOGIC_AXIS width, LOGIC_AXIS height, COLOR color)
{
	CBrush newBrush(color);
	auto oldBrush = pDC->SelectObject(newBrush);
	pDC->Rectangle(x, y, x + width, y + height);
	pDC->SelectObject(oldBrush);
}

void Graphic::DrawRectangleBorder(LOGIC_AXIS x, LOGIC_AXIS y, LOGIC_AXIS width, LOGIC_AXIS height)
{
	auto old = pDC->SelectStockObject(NULL_BRUSH);
	pDC->Rectangle(x, y, x + width, y + height);
	pDC->SelectObject(old);
}

void Graphic::DrawString(std::wstring str, LOGIC_AXIS x, LOGIC_AXIS y)
{
	pDC->TextOutW(x, y, str.c_str());
}

void Graphic::DrawString(std::wstring str, LOGIC_AXIS x, LOGIC_AXIS y, LOGIC_AXIS width, LOGIC_AXIS height)
{
	CRect rect(x, y, x + width, y + height);
	pDC->DrawTextW(str.c_str(), rect, DT_WORDBREAK | DT_EXPANDTABS);
}

void Graphic::DrawTex(LOGIC_AXIS x, LOGIC_AXIS y, LOGIC_AXIS width, LOGIC_AXIS height)
{
	if (tex_id == -1)
	{
		tex_id = SRC::instance.GetTexIdWithPath(L"textures/base/can't find.jpg");
#ifdef DEBUG
		if (tex_id == -1)throw runtime_error("Why???");
#endif
	}
	auto Tex = SRC::instance.GetTexWithId(tex_id);
	Tex.GetImage()->Draw(*pDC, { x, y, x + width, y + height });

}

CDC * Graphic::GetDc()
{
	return pDC;
}
